function toJson(json){
	if(typeof json == 'string'){
		json = $.parseJSON(json);
	}
	// 全局异常信息捕捉
	if(json.errorCode != null || json.errorCode != undefined){
		$.messager.alert("错误",json.errorMsg,"error");
		throw '错误!';
		return false;
	}
	return json;
}

function comboBox(id,url,callfunction,option){
	if(!option) option={};
	$.ajax({
		url:url,
		type:'post',
		success:function(data){
			data = toJson(data);
			$("#"+id).combobox({
				data:data.list,
			    valueField:'ID',
			    textField:'NAME',
			    groupField:option.group||null,
			    height:option.height||30,
			    editable:option.editable||false,
			    panelHeight:'auto'
			});
			if(callfunction)callfunction.call($("#"+id));
		}
	});
}

function comboBoxLocal(id,data,callfunction,option){
	if(!option) option={};
	$("#"+id).combobox({
		data:data,
	    valueField:'ID',
	    textField:'NAME',
	    height:option.height||30,
	    editable:false,
	    panelHeight:'auto'
	});
	if(callfunction)callfunction();
}

// 扩展easyui表单的验证
$.extend($.fn.validatebox.defaults.rules, {    
// 验证IP
 IP: {
        validator: function (value) {
            return /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/.test(value);
        },
        message: '请输入正确的IP地址.'
    },
    // 验证数字非负
    nonnegative : {
        validator: function(value, param){    
            return value>=0;    
        },    
        message: '请输入正整数'   
    },    
    //chunk中大小必须>=1M,<=100G,切为1024倍数
    chunkSize : {
        validator: function(value, param){
            return (value<100*1024*1024)&&(value>1024)&&(value%1024==0);
        },    
        message: '大小必须介于1M到100G之间,且必须能被1024整除，单位为字节'
    }, 
    regstatusCheck:{
        validator: function(value, param){
    		var param = document.getElementById(param).value;
    		if("" !=  param.trim() && "-1" != param.trim() && "REGN_CONTROL" == value.trim())
    		{
    			return false;
    		}else{
    			return true;
    		}
        },    
        message: 'BOL现未停止区ID不能选择REGN_CONTROL'
    },
	NumberCheck: {
		validator: function(value, param) {
			 var s = document.getElementById(param[0]).value;
			 var a=parseInt(s);
			 var b=parseInt(value);
			 if(b>10000){
				 $.fn.validatebox.defaults.rules.NumberCheck.message ="请输入0到4位的非负整数"; 
				 return false;
			 }
			 $.fn.validatebox.defaults.rules.NumberCheck.message ="最大实例数必须大于最小实例数"; 
			return b >= a;
		},
		message: ''
	},
	TimeCheck:{
		 validator:function(value,param){ 
			 var s = $("input[name="+param[0]+"]").val();
			 // 因为日期是统一格式的所以可以直接比较字符串 否则需要Date.parse(_date)转换
			 var start = s.substr(0,7);
			 var end = value.substr(0,7);
			 if(value>=s && start==end)
			 {
				 return true;
			 }
		 },
		 message:'开始时间必须早于结束时间且须在一个月份之内'
	 },
	 TimeChecks:{
		 validator:function(value){
			 var date=new Date();
			 var currentTime=date.getTime();
			
					var dateTemp = value.split(" ");   
				      var tempDate = dateTemp[0].split("-");   
				      var tempTime = dateTemp[1].split(":");   
				      var finalDate = new Date(tempDate[0],tempDate[1]-1,tempDate[2],tempTime[0],tempTime[1],tempTime[2]);
				      $.fn.validatebox.defaults.rules.TimeChecks.message ="下次执行时间必须大于当前时间";
				      try {
			   var dateTemp = value.split(" ");   
			      var tempDate = dateTemp[0].split("-");   
			      var tempTime = dateTemp[1].split(":");   
			      var finalDate = new Date(tempDate[0],tempDate[1]-1,tempDate[2],tempTime[0],tempTime[1],tempTime[2]);
		 var selectedTime=finalDate.getTime();
					 } catch (e) {
				            $.fn.validatebox.defaults.rules.TimeChecks.message ="日期格式不符合！";
							return false;
							}
		 return selectedTime>currentTime;
		 },
		 message:''
	 },
	// 检查日期格式
	 CheckDateTime:{
		 validator:function (value){
	 var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/; 
	 var r = value.match(reg);
	 if(r==null)return false; 
	 r[2]=r[2]-1; 
	 var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]); 
	 if(d.getFullYear()!=r[1])return false; 
	 if(d.getMonth()!=r[2])return false; 
	 if(d.getDate()!=r[3])return false; 
	 if(d.getHours()!=r[4])return false; 
	 if(d.getMinutes()!=r[5])return false; 
	 if(d.getSeconds()!=r[6])return false; 
	 return true; 
	 },  
	 message:'日志格式错误'
	 }
});


// 表格列日期格式化
var Common = {
	    // EasyUI用DataGrid用日期格式化
	    TimeFormatter: function (value, rec, index) {
	        if (value == undefined) {
	            return "";
	        }
	        return value.replace('T',' ');
	    },
	    formatDate: function(timestamp)
	    {
	    	var now = new Date(timestamp-0);
	    	var   year=now.getFullYear();
            var   month=now.getMonth()+1<10?'0'+(now.getMonth()+1):now.getMonth()+1;     
            var   date=now.getDate()<10?'0'+now.getDate():now.getDate();     
            var   hour=now.getHours()<10?'0'+now.getHours():now.getHours();     
            var   minute=now.getMinutes()<10?'0'+now.getMinutes():now.getMinutes();     
            var   second=now.getSeconds()<10?'0'+now.getSeconds():now.getSeconds();     
            return   year+"-"+month+"-"+date+"   "+hour+":"+minute+":"+second;
	    }

	};
/*
$(function(){
	$(document).mousemove(function(e){
	  $("span").text(e.pageX + ", " + e.pageY);
	});
});
	*/

// 子页面改变主页面的高度
var changeHeight = function(height, where) {
	if(height == undefined) 
	{
		return;
	} 
	else 
	{
		if(where === 'main')
		{
			$("#mainFrame").height(height);
			$('.background-div').first().height(height+110);
			$('.background-div').first().height($(window.parent.document).height());
		}
		else
		{
			$("#mainFrame", window.parent.document).height(height-10);
			$('.background-div', window.parent.document).first().height($(window.parent.document).height());
		}
	}
	// 如果高于默认值，则同时改变主页面背景层
	if(height > 560){
	}
}

$(function(){
	$("body").css("visibility","visible"); /*解决easyui加载好之前页面紊乱现象*/
});

// 按钮权限
var privilege = function(mid){
	var tm = top.userMenus;
	if(tm == 'undefined'){
		alert('用户无权使用此功能');
		return;
	}
	// 按钮的标识必须按照类似44050601~44050609这样的规则
	for(var i=1;i<12;i++){
		var buttonID = '';
		if(i<10){
			buttonID = mid + '0' + i;
		}else{
			buttonID = mid + i;
		}
		
		$('[name="' + buttonID + '"]').first().hide(1,function(){
			$.each(tm,function(i,um){
				if(um.id.indexOf(mid) == 0 && um.id != mid){
					$('[name="' + um.id + '"]').first().show();
				}
			});
		});
	}
};

var privilege1 = function(mid){
	var tm = top.userMenus;
	if(tm == 'undefined'){
		alert('用户无权使用此功能');
		return;
	}
	$.each(tm,function(i,um){
		if(um.id.indexOf(mid) == 0 && um.id != mid){
			$('[name="' + um.id + '"]').first().show();
		}
	});
};

//json数组去重,key为需要去重的字段
//使用方法:
//var arr = [{name:1},{name:2},{name:1}];
//var arr1 = arr.distinct('name');
//arr1结果:[{name:1},{name:2}]
//arr保持不变
Array.prototype.distinct = function(key){
	var obj = {};
	return this.filter(function(value,index,array){
		if(obj[value[key]]) return false;
		obj[value[key]] = true;
		return true;
	});
}


var MaskUtil = (function(){  
    var $mask,$maskMsg;  
    var defMsg = '正在处理中...';  
    function init(){  
        if(!$mask){  
            $mask = $("<div class=\"datagrid-mask mymask\"></div>").appendTo("body");  
        }  
        if(!$maskMsg){  
            $maskMsg = $("<div class=\"datagrid-mask-msg mymask\">"+defMsg+"</div>")  
                .appendTo("body").css({'font-size':'12px'});  
        }  
        $mask.css({width:"100%",height:$(document).height()});  
        var scrollTop = $(document.body).scrollTop();  
        $maskMsg.css({  
            left:( $(document.body).outerWidth(true) - 190 ) / 2  
            ,top:( ($(window).height() - 45) / 2 ) + scrollTop  
        });   
    }  
      
    return {  
        mask:function(msg){  
            init();  
            $mask.show();  
            $maskMsg.html(msg||defMsg).show();  
        }  
        ,unmask:function(){  
            $mask.hide();  
            $maskMsg.hide();  
        }  
    }  
}());